// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {

    @property(cc.Node)
    map: cc.Node = null;
    @property(cc.Node)
    player: cc.Node = null;
    @property(cc.Node)
    monster: cc.Node = null;

    playerSpeed = 2000;

    monsterSpeed = 3100;
    monsterSpeedX = 0
    monsterSpeedY = 0
    cd = 0;
    stop: boolean
    startMoveb: boolean
    lastFrameDirX = 0
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        console.log("1")
        this.startMove()
    }

    startMove() {
        this.startMoveb = true;
        let monsterPos = this.monster.getPosition();
        let playerPos = this.player.getPosition();
        // let dir = playerPos.sub(monsterPos);
        // let angle = -dir.signAngle(cc.Vec2.RIGHT)
        // let monsterSpeed = dir.normalize().mul(this.monsterSpeed);
        // let playerSpeed = cc.Vec2.RIGHT.mul(this.playerSpeed)
        // // let playerSpeed = cc.Vec2.RIGHT.mul(this.playerSpeed);
        // let speed = 
        // this.monsterSpeedX = 0;
        this.lastFrameDirX = 0;
        if (playerPos.x < monsterPos.x) {
            // this.monsterSpeedX = this.playerSpeed - this.monsterSpeed;
            this.lastFrameDirX = -1
        } else if (playerPos.x > monsterPos.x) {
            // this.monsterSpeedX = this.playerSpeed + this.monsterSpeed;
            this.lastFrameDirX = 1
        }
        // this.cd = dis / this.monsterSpeed;
        this.cd = 0.5
    }

    update(dt) {
        if (this.stop) return;
        let playerSpeed = cc.Vec2.RIGHT.mul(this.playerSpeed);
        let playerDis = playerSpeed.mul(dt);
        let mapDis = playerDis.mul(-1);
        let mapPos = this.map.getPosition();
        this.map.setPosition(mapPos.add(mapDis))
        let playerPos = this.player.getPosition()
        this.player.setPosition(playerPos.add(playerDis))

        if (!this.startMoveb) return;
        let curDir = 0
        if (this.monster.x > this.player.x) {
            curDir = -1;
        } else if (this.monster.x < this.player.x) {
            curDir = 1;
        }
        
        playerPos = this.player.getPosition();
        let monsterPos = this.monster.getPosition();
        
        let dir = playerPos.sub(monsterPos);
        let dirN = dir.normalize();
        let dis = dir.len();
        this.monsterSpeed = dis / this.cd;
        let monsterSpeed = dirN.mul(this.monsterSpeed);
        let monsterDis = monsterSpeed.mul(dt);
        if(monsterDis.len() >= dis){
            monsterDis = dir
        }
        if (curDir * this.lastFrameDirX <= 0) {
            monsterDis.x = dir.x
        }
        this.monster.setPosition(monsterPos.add(monsterDis));

        this.lastFrameDirX = 0
        if (this.monster.x > this.player.x) {
            this.lastFrameDirX = -1;
        } else if (this.monster.x < this.player.x) {
            this.lastFrameDirX = 1;
        }

        if(this.cd > 0){
            this.cd -= dt;
            if(this.cd <= 0){
                this.stop = true;
                console.log("stop-------",)
                return;
            }
        }
    }

   
}

